package com.lxs.item.controller;


import com.lxs.item.config.FileDfsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class FileController {

    @Autowired
    private FileDfsUtil fileDfsUtil;

    /**
     * 上传文件
     * @param file
     * @return
     */
    @RequestMapping(value = "/uploadFile ", method = RequestMethod.POST, headers="content-type=multipart/form-data")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {

        String result = "00";
        System.out.println(file);
//        try {
//            //获取完整路径  group1/M00/00/00/wKh3gGDJZL-ARhexAAaGIb2yhI462.jpeg
//            String path = fileDfsUtil.upload(file);
//            System.out.println("path:"+path);
//
//            if (StringUtils.isEmpty(path)) {
//                result = "上传失败";
//            } else {
//                result = path;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            result = "服务器异常";
//        }
        return ResponseEntity.ok(result);
    }

    /**
     *
     * @param filePathName
     * @return
     */
    @RequestMapping(value = "/deleteByPath", method = RequestMethod.GET)
    public ResponseEntity<String> deleteByPath(String filePathName) {
        fileDfsUtil.deleteFile(filePathName);
        return ResponseEntity.ok("success delete");
    }

}
